<?php
/**
 * 微信消息记录
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
class applet_noticeLogic {
    /**
     * 获取access token
     * @param int $update   是否更新数据库中的数据
     * @return bool
     */
    public function getAccessToken($update = 0) {

        $_token_info = C('weixin_applet_token_array');
        if (!empty($_token_info) && $update == 0) {
            $_info = unserialize($_token_info);
            if ($_info['end_time'] > TIMESTAMP) {
                return $_info['access_token'];
            }
        }

        $applet_appid = empty(C('weixin_applet_appid')) ? 'wx5d199ac85b0760ab' : C('weixin_applet_appid');
        $applet_secret = empty(C('weixin_applet_secret')) ? '45c8d8bbc2b02f4045ef1717fe33d31d' : C('weixin_applet_secret');

        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$applet_appid.'&secret='.$applet_secret;
        $_token_info = $this->getUrlContents($url);
        if (!empty($_token_info)) {
            $_info = json_decode($_token_info, true);
            $model_setting = Model('setting');
            $_info['end_time'] = TIMESTAMP+$_info['expires_in'];

            $update_array = array();
            $update_array['weixin_applet_token_array'] = serialize($_info);
            $model_setting->updateSetting($update_array,true);
            return $_info['access_token'];
        }
        return false;
    }

    /**
     * 获取模板列表
     */
    public function getAllTemplate($token='',$list=array(),$offset=0,$count=20) {
        if(''==$token){
            $token = $this->getAccessToken();
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token='.$token;
        $data = array(
            'offset' => $offset,
            'count' => $count,
        );
        $get_info = $this->getUrlContents($url,$data);
        $_info = json_decode($get_info, true);
        if (!empty($_info)) {
            $list = array_merge($list,$_info['list']);
            if($count<=count($_info['list'])){
                $list =  $this->getAllTemplate($token,$list,$offset+$count,$count);
            }
        }
        return $list;
    }

    /**
     * 绑定设置
     */
    public function updateTemplate($code,$id) {
        $applet_tpl_list = $this->getAllTemplate();
        $data = array('mp_msg_id'=> $id);
        //更新模板以及例子
        foreach($applet_tpl_list as $v){
            if($v['template_id']==$id){
                $data['wx_content'] = $v['content'];
                $data['wx_example'] = $v['example'];
                break;
            }
        }
        Model('wx_log')->editWxTpl(array('msg_code'=> $code), $data);
        $result = Model('member_msg_tpl')->editMemberMsgTpl(array('mmt_code'=> $code), array('mp_msg_id'=> $id));
        return $result;
    }

    /**
     * 发送模板消息
     */
    public function sendTemplate($token,$msg) {
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token;
        $applet_array = unserialize(base64_decode($msg['log_msg']));
        $post_data = array();
        $post_data['touser'] = $msg['to_id'];//接收者openid
        $post_data['template_id'] = $msg['subject'];//模板ID
        $post_data['page'] = $applet_array['page'];//模板跳转页面
        $post_data['form_id'] = $applet_array['form_id'];//form_id或prepay_id
        unset($applet_array['page']);
        unset($applet_array['form_id']);
        $post_data['data'] = $applet_array;//模板数据
        if (empty($post_data['page'])) $post_data['page'] = 'pages/index/index';//模板跳转链接
        $get_info = $this->getUrlContents($url, $post_data);
        $_info = json_decode($get_info, true);
        return !$_info['errcode'];
    }
    /**
     * 获取模板数据信息
     */
    public function getTemplateData($code,$info) {
        $data = array();
        switch ($code) {

                //待付款提醒
            case "applet_wait_pay":
                $data['keyword1'] = array('value'=> $info['add_time'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['goods_names'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> '您还未付款，即将自动取消','color'=> '#173177');
                $data['keyword4'] = array('value'=> '点击进入订单详情页面完成付款','color'=> '#173177');
                break;

                //订单支付成功通知
            case "applet_pay_success":
                $data['keyword1'] = array('value'=> $info['order_sns'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['payment_time'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> $info['goods_names'],'color'=> '#173177');
                $data['keyword4'] = array('value'=> $info['order_amount'],'color'=> '#173177');
                $data['keyword5'] = array('value'=> "您已付款成功\r\n\r\n首页还有{$info['redpacket_amount']}元优惠券可以领取，快去抢，手慢就没有啦！",'color'=> '#173177');
                break;

                //待发货提醒
            case "applet_wait_deliver":
                $data['keyword1'] = array('value'=> $info['order_sns'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['payment_time'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> $info['goods_names'],'color'=> '#173177');
                $data['keyword4'] = array('value'=> $info['order_amount'],'color'=> '#173177');
                $data['keyword5'] = array('value'=> "您已付款成功\r\n\r\n商城还有小部分拼团及砍价商品做活动，快去抢，手慢就抢光啦！",'color'=> '#173177');
                break;

                //订单取消通知
            case "applet_cancel_order":
                $data['keyword1'] = array('value'=> $info['goods_names'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['cancel_time'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> "您的订单已取消\r\n\r\n🎁可追溯商品，低至9.9元，快来看看！",'color'=> '#173177');
                break;
                //订单发货通知
            case "applet_send_order":
                $data['keyword1'] = array('value'=> $info['goods_names'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['send_time'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> $info['express_name'],'color'=> '#173177');
                $data['keyword4'] = array('value'=> "商品已发货，请注意查收！\r\n\r\n🎁可追溯商品，低至9.9元，快来看看！",'color'=> '#173177');
                break;
                //红包即将到期提醒
            case "applet_redpacket_will_expire":
                $data['keyword1'] = array('value'=> $info['rpacket_title'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['store_name'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> $info['expire_time'],'color'=> '#173177');
                $data['keyword4'] = array('value'=> "您的{$info['rpacket_price']}元优惠红包12小时后过期，马上去用光>>",'color'=> '#173177');
                break;
                //领取成功通知
            case "applet_redpacket_fetch_success":
                $data['keyword1'] = array('value'=> $info['rpacket_title'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['rpacket_owner_name'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> "🎉恭喜您！你已经拆中了{$info['rpacket_price']}元优惠红包，已经放入您的账户内，马上使用>>",'color'=> '#173177');
                break;
                //拼团成功通知
            case "applet_pintuan_success":
                $data['keyword1'] = array('value'=> $info['goods_name'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['pintuan_members'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> "您的好友已帮您拼团成功啦，成功省了{$info['spare_price']}元哦~\r\n\r\n🎁可追溯商品，低至9.9元，快来看看！",'color'=> '#173177');
                break;
                //砍价成功通知
            case "applet_kanjia_success":
                $data['keyword1'] = array('value'=> $info['goods_name'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['kanjia_members'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> "您的好友已帮您砍价成功啦，成功砍了{$info['spare_price']}元哦~\r\n\r\n🎁可追溯商品，低至9.9元，快来看看！",'color'=> '#173177');
                break;
                //砍价人数不足提醒
            case "applet_kanjia_will_fail":
                $data['keyword1'] = array('value'=> $info['goods_name'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['residue_time'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> "您参与的砍价活动即将结束，现在还差{$info['short_price']}元就能成功省{$info['spare_price']}元！快邀请好友帮你一起砍价吧！",'color'=> '#173177');
                break;
                //砍价失败通知
            case "applet_kanjia_fail":
                $data['keyword1'] = array('value'=> $info['order_sn'],'color'=> '#173177');
                $data['keyword2'] = array('value'=> $info['goods_name'],'color'=> '#173177');
                $data['keyword3'] = array('value'=> $info['order_amount'],'color'=> '#173177');
                $data['keyword4'] = array('value'=> "您参加的砍价活动因为砍价金额未到底价而失败，我们将在1~2个工作日内为您安排自动退款\r\n\r\n🎁可追溯商品，低至9.9元，快来看看！",'color'=> '#173177');
                break;
            default:
                break;
        }
        return $data;
    }

    /**
     * 接口请求
     * @param string $url
     * @return string
     */
    public function getUrlContents($url,$post_data = array()){
        $ch = curl_init();
        //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);
        if(!empty($post_data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            if(is_array($post_data)) $post_data = json_encode($post_data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * 订单取消通知
     * @param $order_id
     * @param int $buyer_id
     * @param int $cancel_time
     */
    public function appletCancelOrder($order_id){
        $model_order = Model('order');
        $model_member = Model('member');
        $order_info = $model_order->getOrderInfo(array('order_id'=>$order_id),array(),array('buyer_id','order_from','order_state'));
        if((ORDER_FROM_APPLET!=$order_info['order_from']) || (0!=$order_info['order_state'])) return;
        $buyer_id = $order_info['buyer_id'];
        //订单取消时间：从order_log表获取，因为取消订单的时候回向该表写入一条处理记录
        $cancel_time = $model_order->table('order_log')->where(array('order_id'=>$order_id,'log_orderstate'=>0))->field(array('log_time'))->find();
        $cancel_time = $cancel_time['log_time'];
        $cancel_time = date('Y年m月d日 H:i',$cancel_time);

        $goods_names = $model_order->getOrderGoodsList(array('order_id'=>$order_id),array('goods_name'));
        $goods_names = array_column($goods_names,'goods_name');
        $goods_names = array_unique($goods_names);
        $goods_names = implode('，',$goods_names);
        $info = array(
            'cancel_time' => $cancel_time,
            'goods_names' => $goods_names
        );
        $param1['code'] = 'applet_cancel_order';
        $param1['member_id'] = $buyer_id;
        $param1['param']['applet_array'] = $this->getTemplateData($param1['code'],$info);
        $param1['param']['applet_array']['form_id'] = $model_member->subtractFormId(array('member_id'=>$param1['member_id']));
        $param1['param']['applet_array']['page'] = 'pages/index/index';
        RealTimePush('sendMemberMsg', $param1);
    }

    /**
     * 待付款提醒
     * @param $order_id
     * @param $buyer_id
     * @param $add_time
     */
    public function appletWaitPay($order_id,$buyer_id,$add_time){
        $model_order = Model('order');

        $order_info = $model_order->getOrderInfo(array('order_id'=>$order_id),array(),array('order_from','pay_sn'));
        if(ORDER_FROM_APPLET!=$order_info['order_from']) return;

        $model_member = Model('member');
        $goods_names = $model_order->getOrderGoodsList(array('order_id'=>$order_id),['goods_name']);
        $goods_names = array_column($goods_names,'goods_name');
        $goods_names = array_unique($goods_names);
        $goods_names = implode('，',$goods_names);
        $info = array(
            'add_time' => date('Y年m月d日 H:i',$add_time),
            'goods_names' => $goods_names
        );

        $param1['code'] = 'applet_wait_pay';
        $param1['member_id'] = $buyer_id;

        $param1['param']['applet_array'] = $this->getTemplateData($param1['code'],$info);
        $param1['param']['applet_array']['form_id'] = $model_member->subtractFormId(array('member_id'=>$param1['member_id']));
        $param1['param']['applet_array']['page'] = 'pages/order/order-detail/index?order_id='.$order_id.'&pay_sn='.$order_info['pay_sn'];

        RealTimePush('sendMemberMsg', $param1);
    }

    /**
     * //发送支付成功通知和待发货通知
     * @param $order_sn
     * @param $time_end
     * @param $cash_fee
     */
    public function applet_pay_success_applet_wait_deliver($order_sn,$time_end,$cash_fee){
        //发送服务通知
        $model_order = Model('order');
        $condition['pay_sn'] = $order_sn;
        $fields = array('order_id','order_sn','buyer_id','order_type','order_from');
        $order_list = $model_order->getOrderList($condition,'',$fields);
        if(ORDER_FROM_APPLET!=$order_list[0]['order_from']) return;

        $order_sns_arr = array_column($order_list,'order_sn');
        $order_ids_arr = array_column($order_list,'order_id');

        $goods_names = $model_order->getOrderGoodsList(array('order_id'=>array('in',$order_ids_arr)),['goods_name']);
        $goods_names = array_column($goods_names,'goods_name');
        $goods_names = array_unique($goods_names);

        $goods_names = implode('，',$goods_names);
        $order_sns = implode(',',$order_sns_arr);
        $payment_time = date('Y年m月d日 H:i',strtotime($time_end));
        $order_amount = $cash_fee/100;

        $info = array(
            'order_sns' => $order_sns,
            'payment_time' => $payment_time,
            'goods_names' => $goods_names,
            'order_amount' => $order_amount.'元',
        );

        $model_member = Model('member');

        $param1['code'] = 'applet_pay_success';
        $param1['member_id'] = $order_list[0]['buyer_id'];

        $redpacket_amount_condition = array(
            'rpacket_t_start_date' => array(
                'lt',
                TIMESTAMP
            ),
            'rpacket_t_end_date' => array(
                'gt',
                TIMESTAMP
            ),
            'rpacket_t_place' => 3,
        );
        $redpacket_amount = Model('redpacket')->getRedpacketTemplateamount($redpacket_amount_condition);
        $param1['param']['applet_array'] = $this->getTemplateData($param1['code'],array_merge($info,array('redpacket_amount'=>$redpacket_amount)));
        $param1['param']['applet_array']['form_id'] = $model_member->subtractFormId(array('member_id'=>$param1['member_id']));
        $param1['param']['applet_array']['page'] = 'pages/index/index';
        RealTimePush('sendMemberMsg', $param1);

        //下面是待发货通知
        //订单类型:1普通订单(默认),2预定订单,3门店自提,4拼团订单,5门店配送
        if(in_array($order_list[0]['order_type'],array(1,5))){
            $param2['code'] = 'applet_wait_deliver';
            $param2['member_id'] = $order_list[0]['buyer_id'];

            $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
            $param2['param']['applet_array']['form_id'] = $model_member->subtractFormId(array('member_id'=>$param2['member_id']));
            $param2['param']['applet_array']['page'] = 'pages/index/index';
            RealTimePush('sendMemberMsg', $param2);
        }

    }

    /**
     * 订单发货通知
     * @param $order_id
     * @param $buyer_id
     * @param $send_time
     * @param $express_name
     */
    public function appletSendOrder($order_id,$buyer_id,$send_time,$express_name){
        $model_order = Model('order');
        $model_member = Model('member');

        if(ORDER_FROM_APPLET!=$model_order->getOrderInfo(array('order_id'=>$order_id),array(),array('order_from'))['order_from']) return;

        $goods_names = $model_order->getOrderGoodsList(array('order_id'=>$order_id),['goods_name']);
        $goods_names = array_column($goods_names,'goods_name');
        $goods_names = array_unique($goods_names);
        $goods_names = implode('，',$goods_names);
        $info = array(
            'goods_names' => $goods_names,
            'send_time' => date('Y年m月d日 H:i',$send_time),
            'express_name' => $express_name,
        );

        $param1['code'] = 'applet_send_order';
        $param1['member_id'] = $buyer_id;

        $param1['param']['applet_array'] = $this->getTemplateData($param1['code'],$info);
        $param1['param']['applet_array']['form_id'] = $model_member->subtractFormId(array('member_id'=>$param1['member_id']));
        $param1['param']['applet_array']['page'] = 'pages/index/index';

        $update_pay_notice = $model_order->editOrder(array('deliver_notice'=>1),array('order_id'=>$order_id));

        if($update_pay_notice){
            RealTimePush('sendMemberMsg', $param1);
        }
    }

    /**
     * 红包（小程序优惠券）即将到期通知
     * @param $val
     */
    public function appletRedpacketWillExpire($val){
        if(3!=$val['rpacket_place']) return;
        $rpacket_title = $val['rpacket_title'];
        $store_name = '中国追溯商城';
        $expire_time = date('Y年m月d日',$val['rpacket_end_date']);
        $rpacket_price = $val['rpacket_price'];
        $member_id = $val['rpacket_owner_id'];

        $info = array(
            'rpacket_title' => $rpacket_title,
            'store_name' => $store_name,
            'expire_time' => $expire_time,
            'rpacket_price' => $rpacket_price,
        );

        $param2['code'] = 'applet_redpacket_will_expire';
        $param2['member_id'] = $member_id;

        $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
        $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
        $param2['param']['applet_array']['page'] = 'pages/discount/index';
        RealTimePush('sendMemberMsg', $param2);

    }

    /**
     * 领取成功通知（红包）
     * @param $rpacket_id   redpacket表中的rpacket_id
     */
    public function appletRedpacketFetchSuccess($rpacket_id){

        $rpacket_info = Model('redpacket')->getRedpacketInfo(array('rpacket_id'=>$rpacket_id),array('rpacket_place','rpacket_title','rpacket_owner_name','rpacket_price','rpacket_owner_id'));

        if(!in_array($rpacket_info['rpacket_place'],array(0,3))) return;

        $info = array(
            'rpacket_title' => $rpacket_info['rpacket_title'],
            'rpacket_owner_name' => $rpacket_info['rpacket_owner_name'],
            'rpacket_price' => $rpacket_info['rpacket_price'],
        );

        $param2['code'] = 'applet_redpacket_fetch_success';
        $param2['member_id'] = $rpacket_info['rpacket_owner_id'];

        $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
        $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
        $param2['param']['applet_array']['page'] = 'pages/index/index';

        RealTimePush('sendMemberMsg', $param2);
    }

    /**
     * 拼团成功通知
     * @param $pay_sn   支付单号
     */
    //拼团订单支付完成后会检查是否已经拼团成功，如果拼团成功会调用此方法向所有参团人发送通知
    //支付成功会传过来一个pay_sn
    //用pay_sn在orders表查出order_id
    //用order_id在order_pintuan表查出log_id
    //表order_pintuan中表示：如果是团长buyer_type字段将等于0，如果不是团长就等于团长的那条数据的log_id
    //所以在此得出该拼团活动中的所有参团人员
    public function appletPintuanSuccess($pay_sn){
        $model_order = Model('order');
        $order_info = $model_order->getOrderInfo(array('pay_sn'=>$pay_sn),array(),array('order_id','order_from','order_type'));
        if((ORDER_FROM_APPLET!=$order_info['order_from']) || (4!=$order_info['order_type'])) return;

        $model_p_pintuan = Model('p_pintuan');
        $order_id = $order_info['order_id'];
        $fields = array('log_id','buyer_type','buyer_id','min_num','goods_id','buyer_name','add_time');
        $order_pintuan_info = $model_p_pintuan->getOrderInfo(array('order_id'=>$order_id),$fields);
        if(0==$order_pintuan_info['buyer_type']){
            $order_pintuan_list = $model_p_pintuan->getOrderList(array('buyer_type'=>$order_pintuan_info['log_id']),null,'log_id desc',$fields);

            array_unshift($order_pintuan_list,$order_pintuan_info);
        }
        $pintuan_num = count($order_pintuan_list);
        //如果没有拼成功
        if($pintuan_num<$order_pintuan_info['min_num']){
            return;
        }

        $goods_info = Model('goods')->getGoodsInfoByID($order_pintuan_info['goods_id'],array('goods_name'));

        //获取活动当时商品的原价格和拼团价格
        $p_pintuan_info = $model_p_pintuan->getGoodsInfo(array('start_time'=>array('elt',$order_pintuan_info['add_time']),'end_time'=>array('egt',$order_pintuan_info['add_time']),'goods_id'=>$order_pintuan_info['goods_id']),array('goods_price','pintuan_price'));

        //计算差价
        $spare_price = $p_pintuan_info['goods_price']-$p_pintuan_info['pintuan_price'];

        $pintuan_members = implode(',',array_unique(array_column($order_pintuan_list,'buyer_name')));

        $order_pintuan_buyer_ids = array_unique(array_column($order_pintuan_list,'buyer_id'));
        foreach($order_pintuan_buyer_ids as $buyer_id){
            $info = array(
                'goods_name' => $goods_info['goods_name'],
                'pintuan_members' => $pintuan_members,
                'spare_price' => $spare_price,
            );

            $param2['code'] = 'applet_pintuan_success';
            $param2['member_id'] = $buyer_id;

            $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
            $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
            $param2['param']['applet_array']['page'] = 'pages/index/index';
            RealTimePush('sendMemberMsg', $param2);
        }
    }

    /**
     * 砍价成功，跟拼团成功差不多
     * @param $pay_sn
     */
    public function appletKanjiaSuccess($order_sn){
        $model_order = Model('order');
        $order_info = $model_order->getOrderInfo(array('order_sn'=>$order_sn),array(),array('order_id','order_from','order_type'));
        if((ORDER_FROM_APPLET!=$order_info['order_from']) || (200!=$order_info['order_type'])) return;

        $order_id = $order_info['order_id'];
        $model_p_kanjia = Model('p_kanjia');
        //获取order_kanjia表的信息
        $order_kanjia_info = $model_p_kanjia->getOrderInfo(array('order_id'=>$order_id),array('goods_id','add_time','kanjia_price','status','buyer_id'));

        //如果砍价还没成功
        if(1!=$order_kanjia_info['status']){
            return;
        }

        $order_kanjia_info['kanjia_price'] = json_decode($order_kanjia_info['kanjia_price'],true);

        $goods_info = Model('goods')->getGoodsInfoByID($order_kanjia_info['goods_id'],array('goods_name'));

        //获取活动当时商品的原价格和砍价价格
        //获取p_kanjia_goods表的信息
        $p_kanjia_info = $model_p_kanjia->getGoodsInfo(array('start_time'=>array('elt',$order_kanjia_info['add_time']),'end_time'=>array('egt',$order_kanjia_info['add_time']),'goods_id'=>$order_kanjia_info['goods_id']),array('goods_price','kanjia_price'));

        //计算差价
        $spare_price = $p_kanjia_info['goods_price']-$p_kanjia_info['kanjia_price'];

        $kanjia_members = implode(',',array_unique(array_column($order_kanjia_info['kanjia_price']['people'],'name')));

        $info = array(
            'goods_name' => $goods_info['goods_name'],
            'kanjia_members' => $kanjia_members,
            'spare_price' => $spare_price,
        );

        $param2['code'] = 'applet_kanjia_success';
        $param2['member_id'] = $order_kanjia_info['buyer_id'];

        $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
        $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
        $param2['param']['applet_array']['page'] = 'pages/index/index';

        RealTimePush('sendMemberMsg', $param2);
    }


    /**
     * 砍价人数不足提醒
     * @param $order_kanjia_info
     */
    public function appletKanjiaWillFail($order_kanjia_info){

        $model_p_kanjia = Model('p_kanjia');
        $residue_time = date_diff(date_create(date('Y-m-d H:i:s',TIMESTAMP)),date_create(date('Y-m-d H:i:s',$order_kanjia_info['end_time'])));

        //获取活动当时商品的原价格和砍价价格
        //获取p_kanjia_goods表的信息
        $p_kanjia_info = $model_p_kanjia->getGoodsInfo(array('start_time'=>array('elt',$order_kanjia_info['add_time']),'end_time'=>array('egt',$order_kanjia_info['add_time']),'goods_id'=>$order_kanjia_info['goods_id']),array('goods_price','kanjia_price','goods_name'));
        //计算差价
        $spare_price = $p_kanjia_info['goods_price']-$p_kanjia_info['kanjia_price'];
        //砍价信息
        $kan_price_info = (array)json_decode($order_kanjia_info['kanjia_price'],true);
        //已砍次数
        $kan_price_count = $kan_price_info['now'];
        //已砍价格
        $kan_price = array_sum(array_slice($kan_price_info['kan_price'],$kan_price_count));

        $info = array(
            'goods_name' => $p_kanjia_info['goods_name'],
            'residue_time' => "{$residue_time->d}天{$residue_time->h}小时{$residue_time->i}分",    //还剩多少时间
            'short_price' => $spare_price-$kan_price,   //还差多少
            'spare_price' => $spare_price,  //砍完后可以省多少
        );

        $param2['code'] = 'applet_kanjia_will_fail';
        $param2['member_id'] = $order_kanjia_info['buyer_id'];

        $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
        $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
        $param2['param']['applet_array']['page'] = 'pages/odd/index';
        RealTimePush('sendMemberMsg', $param2);

    }

    /**
     * 砍价失败通知
     * @param $order_sn 订单编号
     */
    public function appletKanjiaFail($order_sn){

        $model_p_kanjia = Model('p_kanjia');

        $order_info = Model()->table('order_kanjia,orders,goods')->where('order_kanjia.order_sn='.$order_sn)
            ->on('orders.order_id=order_kanjia.order_id,order_kanjia.goods_id=goods.goods_id')
            ->field('order_kanjia.kanjia_price,order_kanjia.log_id,order_kanjia.order_id,order_kanjia.buyer_id,order_kanjia.add_time,order_kanjia.end_time,order_kanjia.goods_id,orders.pay_sn,orders.order_amount,goods.goods_name')
            ->find();


        $info = array(
            'order_sn' => $order_sn,
            'order_amount' => $order_info['order_amount'],
            'goods_name' => $order_info['goods_name'],
        );

        $param2['code'] = 'applet_kanjia_fail';
        $param2['member_id'] = $order_info['buyer_id'];

        $param2['param']['applet_array'] = $this->getTemplateData($param2['code'],$info);
        $param2['param']['applet_array']['form_id'] = Model('member')->subtractFormId(array('member_id'=>$param2['member_id']));
        $param2['param']['applet_array']['page'] = 'pages/joinbargain/index?order_id='.$order_info['order_id'] . '&pay_sn=' . $order_info['pay_sn'] . '&log_id=' . $order_info['log_id'] . '&buyer_id=' . $order_info['buyer_id'];
        RealTimePush('sendMemberMsg', $param2);
    }
}